#!/usr/bin/env python
import pexpect
import os
import urllib
import sys
import re
def youdao(keyword):
	url='http://www.youdao.com/w/eng/'+keyword
	page=urllib.urlopen(url).read()
	find_result=re.findall(r'<div class="trans-container">(.*?)</div>',page,re.S|re.M)

	return_string=find_result[0].strip()
	return_string=re.sub('<(.*?)>','',return_string).strip()
	num=max(map(len,return_string.split('\n')))
	print(''.join(['*']*num))
	print return_string
	print(''.join(['*'*num]))
        return '\n'+keyword+' : '+return_string+'\n'
def git(file):
    pass

def writelog(st,path):
    with open(path,'a')as f:
	f.write(st)

def logfile( st):
    return get_now_time()+' '+get_my_ip()+': '+st+'\n'

import socket 
import datetime
def get_my_ip(): 
	try:
		csock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 
		csock.connect(('8.8.8.8', 80)) 
		(addr, port) = csock.getsockname() 
		csock.close() 
		return addr 
	except socket.error: 
		return "127.0.0.1" 
#print get_my_ip()

def get_now_time():
	return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
#print logfile('hello world')

writelog(logfile(youdao(sys.argv[1])),'../word.txt')


def git_add_commit(path):
  #os.system('git status')
    os.system ('git add '+path+'word.txt')
    os.system('git status')
    os.system('git commit -m "test my code"')
    print 'commit OK!'

def git_pull(path):
  cmd='git pull -r'
  child=pexpect.spawn(cmd)
  try:
    i=child.expect(["Username for 'https://git.oschina.net':","$"])
    print i
    if i==0:
	child.sendline('zhangjiahao234')
        child.expect("Password for 'https://zhangjiahao234@git.oschina.net':")
        child.sendline('zhangjiahao123')
        child.interact()
        print 'git pull -r OK ,use username and password'
    elif i==1:
        print 'git pull -r OK!without username and password'
        child.interact()
  except pexpect.EOF:
          print 'child exit!'
  except pexpect.TIMEOUT:  
          print 'time out' 
   

def git_push(path):   
  cmd='git push origin master'
  child=pexpect.spawn(cmd)
  try:
    i=child.expect(["Username for 'https://git.oschina.net':","$"])
    if i==0:
            child.sendline('zhangjiahao234')
            child.expect("Password for 'https://zhangjiahao234@git.oschina.net':")
            child.sendline('zhangjiahao123')
            child.interact()
            print 'git push  OK ,use username and password'
    elif i==1:
        print 'git pull  OK!without username and password'
        child.interact()
  except pexpect.EOF:
          print 'child exit!'
  except pexpect.TIMEOUT:  
          print 'time out'    

git_add_commit('../')
git_pull('../')
git_push('../')
